package com.miplus.ccrm.core.controller;

import com.miplus.ccrm.common.CmdResVo;
import com.miplus.ccrm.common.JSysUser;
import com.miplus.ccrm.common.PageData;
import com.miplus.ccrm.common.R;
import com.miplus.ccrm.core.controller.vo.LabelResVo;
import com.miplus.ccrm.core.controller.vo.conf.*;
import com.miplus.ccrm.core.controller.vo.conf.DistBatchReqVo;
import com.miplus.ccrm.core.controller.vo.conf.DistBatchResVo;
import com.miplus.ccrm.core.security.JCommonData;
import com.miplus.ccrm.core.service.conf.*;
import com.miplus.ccrm.core.utils.SpringContextUtil;
import lombok.extern.slf4j.Slf4j;
import org.jetbrains.annotations.NotNull;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * 2-1.配置服务接口
 */
@Slf4j
@CrossOrigin(origins = "*", maxAge = 3600)
@RestController
@RequestMapping("/conf")
public class ConfController {
    @Resource
    private MsgDefineService msgDefineService;
    @Resource
    private WfWorkflowService wfWorkflowService;
    @Resource
    private WfStepsService wfStepsService;
    @Resource
    private WfActionsService wfActionsService;
    @Resource
    private MsgAnnoucesService msgAnnoucesService;
    @Resource
    private SysOutsourcingService sysOutsourcingService;
    @Resource
    private SysPostService sysPostService;
    @Resource
    private WfCasesService wfCasesService;
    @Resource
    private WfCaseactionsService wfCaseactionsService;
    @Resource
    private FiDistbatchHistService fiDistbatchHistService;
    @Resource
    private MsgAlertsService msgAlertsService;
    @Resource
    private FiPenaltyHistService fiPenaltyHistService;
    @Resource
    private JCommonData jCommonData;
    @Resource
    private FiCommissionService fiCommissionService;
    @Resource
    private FiTimeliveSetService timeliveSetService;

    /**
     * 取消息模板信息LIST
     */
    @GetMapping("/msgTempList")
    public R<PageData<MsgTmpResVo>> msgTempList(@Validated MsgTmpReqVo msgTmpReqVo){
        return R.SuccessPage(msgDefineService.msgTempList(msgTmpReqVo));
    }

    /**
     * 取消息模板详细信息One
     */
    @GetMapping("/getMsgTmpInfo")
    public R<MsgTmpResVo> getMsgTmpInfo(@RequestParam @NotNull String tempID){
        return R.Success(msgDefineService.getMsgTmpInfo(tempID));
    }

    /**
     * 取消息模板Value.LIST
     */
    @GetMapping("/getMsgTmpList")
    public R<List<LabelResVo>> getMsgTmpList(){
        return R.Success(msgDefineService.getMsgTmpList());
    }

    /**
     * 添加消息模板
     */
    @PostMapping("/msgTempInsert")
    public R<CmdResVo> insertMsgTmp(@RequestBody @Validated MsgTmpEditReqVo msgTmpEditReqVo) {
        return R.Success(msgDefineService.insertMsgTmp(msgTmpEditReqVo));
    }

    /**
     * 修改消息模板
     */
    @PostMapping("/msgTempUpdate")
    public R<CmdResVo> updateMsgTmp(@RequestBody @Validated MsgTmpEditReqVo msgTmpEditReqVo) {
        return R.Success(msgDefineService.updateMsgTmp(msgTmpEditReqVo));
    }

    /**
     * 删除消息模板
     */
    @PostMapping("/msgTempDelete")
    public R<CmdResVo> deleteMsgTmp(@RequestBody @Validated DeleteReqVo deleteReqVo) {
        return R.Success(msgDefineService.deleteMsgTmp(deleteReqVo.getId()));
    }

    /**
     * 更新消息模板状态
     */
    @PostMapping("/msgTmpStatusUpdate")
    public R<CmdResVo> updateMsgTmpStatus(@RequestBody @Validated UpdateStateReqVo updateStateReqVo) {
        return R.Success(msgDefineService.updateStatus(updateStateReqVo.getId(),updateStateReqVo.getStatus()));
    }

    /**
     * 取工作流LIST
     */
    @GetMapping("/workFlowList")
    public R<PageData<WfResVo>> workFlowList(@Validated WfReqVo wfReqVo){
        return R.SuccessPage(wfWorkflowService.workFlowList(wfReqVo));
    }

    /**
     * 取工作流Value.LIST
     */
    @GetMapping("/getWorkFlowList")
    public R<List<LabelResVo>> getWorkFlowList(){
        return R.Success(wfWorkflowService.getWorkFlowList());
    }

    /**
     * 取工作流节点状态字典
     */
    @GetMapping("/getNamesValuesByWfid")
    public R<List<LabelResVo>> getNamesValuesByWfid(@RequestParam @NotNull Integer wfid){
        return R.Success(wfWorkflowService.getNamesValuesByWfid(wfid));
    }

    /**
     * 添加工作流
     */
    @PostMapping("/insertFlowList")
    public R<CmdResVo> insertFlowList(@RequestBody @Validated WfEditReqVo wfEditReqVo) {
        String usrid= jCommonData.getCurrUsrID();
        return R.Success(wfWorkflowService.insertWorkFlow(usrid,wfEditReqVo));
    }

    /**
     * 修改工作流
     */
    @PostMapping("/updateFlowList")
    public R<CmdResVo> updateFlowList(@RequestBody @Validated WfEditReqVo wfEditReqVo) {
        String usrid= jCommonData.getCurrUsrID();
        return R.Success(wfWorkflowService.updateWorkFlow(usrid,wfEditReqVo));
    }

    /**
     * 删除工作流
     */
    @PostMapping("/deleteFlowList")
    public R<CmdResVo> deleteFlowList(@RequestBody @Validated DeleteReqVo deleteReqVo) {
        return R.Success(wfWorkflowService.deleteWorkFlow(deleteReqVo.getId()));
    }

    /**
     * 取工作流节点LIST
     */
    @GetMapping("/wfStepList")
    public R<PageData<WfStepResVo>> wfStepList(@Validated WfStepReqVo wfStepReqVo){
        return R.SuccessPage(wfStepsService.wfStepList(wfStepReqVo));
    }

    /**
     * 取工作流下一个节点LIST
     */
    @GetMapping("/getStepIdList")
    public R<List<LabelResVo>> getStepIdList(@RequestParam @NotNull Integer wfid){
        return R.Success(wfStepsService.getStepIdList(wfid));
    }

    /**
     * 添加工作流节点
     */
    @PostMapping("/insertWfStep")
    public R<CmdResVo> insertWfStep(@RequestBody @Validated WfStepEditReqVo wfStepEditReqVo) {
        return R.Success(wfStepsService.insertStep(wfStepEditReqVo));
    }

    /**
     * 修改工作流节点
     */
    @PostMapping("/updateWfStep")
    public R<CmdResVo> updateWfStep(@RequestBody @Validated WfStepEditReqVo wfStepEditReqVo) {
        return R.Success(wfStepsService.updateStep(wfStepEditReqVo));
    }

    /**
     * 删除工作流节点
     */
    @PostMapping("/deleteWfStep")
    public R<CmdResVo> deleteWfStep(@RequestBody @Validated DeleteReqVo deleteReqVo) {
        return R.Success(wfStepsService.deleteStep(deleteReqVo.getId()));
    }

    /**
     * 取工作流动作LIST
     */
    @GetMapping("/wfActionList")
    public R<PageData<WfActionResVo>> wfActionList(@Validated WfActionReqVo wfActionReqVo){
        return R.SuccessPage(wfActionsService.wfActionList(wfActionReqVo));
    }

    /**
     * 添加工作流动作
     */
    @PostMapping("/insertWfAction")
    public R<CmdResVo> insertWfAction(@RequestBody @Validated WfActEditReqVo wfActEditReqVo) {
        return R.Success(wfActionsService.insertAction(wfActEditReqVo));
    }

    /**
     * 修改工作流动作
     */
    @PostMapping("/updateWfAction")
    public R<CmdResVo> updateWfAction(@RequestBody @Validated WfActEditReqVo wfActEditReqVo) {
        return R.Success(wfActionsService.updateAction(wfActEditReqVo));
    }

    /**
     * 删除工作流动作
     */
    @PostMapping("/deleteWfAction")
    public R<CmdResVo> deleteWfAction(@RequestBody @Validated DeleteReqVo deleteReqVo) {
        return R.Success(wfActionsService.deleteAction(deleteReqVo.getId()));
    }

    /**
     * 取工作流实例动作ID.LIST
     */
    @GetMapping("/getActionList")
    public R<List<WfActResVo>> getActionList(@Validated WfActReqVo wfActReqVo){
        JSysUser user= jCommonData.getCurrUsr();
        return R.Success(wfCasesService.getActionList(user,wfActReqVo));
    }

    /**
     * 取工作流实例过程
     */
    @GetMapping("/wfProcessList")
    public R<List<WfCaseProcResVo>> wfProcessList(@Validated WfCaseProcReqVo wfCaseProcReqVo){
        return R.Success(wfCaseactionsService.wfProcessList(wfCaseProcReqVo));
    }

    /**
     * 取公告管理LIST
     */
    @GetMapping("/annouceList")
    public R<PageData<AnnoResVo>> msgAnnouceList(@Validated AnnoReqVo annoReqVo){
        String usrid= jCommonData.getCurrUsrID();
        return R.SuccessPage(msgAnnoucesService.msgAnnoList(usrid,annoReqVo));
    }

    /**
     * 取公告查看LIST
     */
    @GetMapping("/annouceViewList")
    public R<PageData<AnnoResVo>> msgAnnoViewList(@Validated AnnoReqVo annoReqVo){
        //String usrid= jCommonData.getCurrUsrID();
        //return R.SuccessPage(msgAnnoucesService.msgAnnoViewList(usrid,annoReqVo));
        return R.SuccessPage(null);
    }

    /**
     * 取公告详细
     */
    @GetMapping("/annouceInfo")
    public R<AnnoResVo> msgAnnouceInfo(@RequestParam @NotNull Integer annouceid){
        return R.Success(msgAnnoucesService.msgAnnoInfo(annouceid));
    }

    /**
     * 添加公告
     */
    @PostMapping("/anouceInsert")
    public R<CmdResVo> insertAnnouce(@RequestBody @Validated AnnoEditReqVo annoEditReqVo) {
        String usrid= jCommonData.getCurrUsrID();
        return R.Success(msgAnnoucesService.insertAnno(usrid,annoEditReqVo));
    }

    /**
     * 修改公告
     */
    @PostMapping("/annouceUpdate")
    public R<CmdResVo> updateAnnouce(@RequestBody @Validated AnnoEditReqVo annoEditReqVo) {
        String usrid= jCommonData.getCurrUsrID();
        return R.Success(msgAnnoucesService.updateAnno(usrid,annoEditReqVo));
    }

    /**
     * 删除公告
     */
    @PostMapping("/annouceDelete")
    public R<CmdResVo> deleteAnnouce(@RequestBody @Validated DeleteReqVo deleteReqVo) {
        return R.Success(msgAnnoucesService.deleteAnno(Integer.parseInt(deleteReqVo.getId())));
    }

    /**
     * 取消息GRID
     */
    @GetMapping("/alertViewList")
    public R<PageData<AlertResVo>> msgAlertViewList(@Validated AlertReqVo alertReqVo){
        //String usrid= jCommonData.getCurrUsrID();
        //return R.SuccessPage(msgAlertsService.msgAlertViewList(usrid,alertReqVo));
        return R.SuccessPage(null);
    }

    /**
     * 消息已读
     */
    @PostMapping("/alertReaded")
    public R<CmdResVo> msgAlertReaded(@RequestBody @Validated AlertReadVo alertReadVo) {
        return R.Success(msgAlertsService.msgAlertReaded(alertReadVo.getAlertid()));
    }

    /**
     * 公告已读
     */
    @PostMapping("/annouceReaded")
    public R<CmdResVo> annouceReaded(@RequestBody @Validated AnnoReadVo annoReadVo) {
        String usrid= jCommonData.getCurrUsrID();
        return R.Success(msgAnnoucesService.annouceReaded(usrid,annoReadVo.getAnnouceid()));
    }

    /**
     * 取我可看的未读公告数
     */
    @GetMapping("/getAnnoNoReadCount")
    public R<CmdResVo> getAnnoNoReadCount(){
        String usrid= jCommonData.getCurrUsrID();
        return R.Success(msgAnnoucesService.getAnnoNoReadCount(usrid));
    }

    /**
     * 取我的未读消息数
     */
    @GetMapping("/getAlertNoReadCount")
    public R<CmdResVo> getAlertNoReadCount(){
        String usrid= jCommonData.getCurrUsrID();
        return R.Success(msgAlertsService.getAlertNoReadCount(usrid));
    }


    /**
     * 取委外机构LIST
     */
    @GetMapping("/outsourcingList")
    public R<PageData<OutGrpResVo>> outsourcingList(@Validated OutGrpReqVo outGrpReqVo){
        String usrid= jCommonData.getCurrUsrID();
        return R.SuccessPage(sysOutsourcingService.outSourcingList(usrid,outGrpReqVo));
    }

    /**
     * 添加委外机构
     */
    @PostMapping("/outsourcingInsert")
    public R<CmdResVo> outsourcingInsert(@RequestBody @Validated OutGrpEditVo outGrpEditVo) {
        String usrid= jCommonData.getCurrUsrID();
        return R.Success(sysOutsourcingService.outSourcingInsert(usrid,outGrpEditVo));
    }

    /**
     * 修改委外机构
     */
    @PostMapping("/outsourcingUpdate")
    public R<CmdResVo> outsourcingUpdate(@RequestBody @Validated OutGrpEditVo outGrpEditVo) {
        String usrid= jCommonData.getCurrUsrID();
        return R.Success(sysOutsourcingService.outSourcingUpdate(usrid,outGrpEditVo));
    }

    /**
     * 删除委外机构
     */
    @PostMapping("/outsourcingDelete")
    public R<CmdResVo> outsourcingDelete(@RequestBody @Validated OutGrpIDVo outGrpIDVo) {
        return R.Success(sysOutsourcingService.outSourcingDelete(outGrpIDVo.getGrpid()));
    }

    /**
     * 修改委外机构状态
     */
    @PostMapping("/outsourcingSetStatus")
    public R<CmdResVo> outsourcingSetStatus(@RequestBody @Validated OutGrpStatusVo outGrpStatusVo) {
        String usrid= jCommonData.getCurrUsrID();
        return R.Success(sysOutsourcingService.outSourcingSetStatus(
                usrid,outGrpStatusVo.getGrpid(),outGrpStatusVo.getStatus()
        ));
    }

    /**
     * 取地址省LIST
     */
    @GetMapping("/getProvinceList")
    public R<List<LabelResVo>> getProvinceList(){
        return R.Success(sysPostService.getProvinceList());
    }

    /**
     * 取省对应的市LIST
     */
    @GetMapping("/getCityList")
    public R<List<LabelResVo>> getCityList(@RequestParam @NotNull String province){
        return R.Success(sysPostService.getCityList(province));
    }

    /**
     * 取市对应的区县LIST
     */
    @GetMapping("/getDistrictList")
    public R<List<LabelResVo>> getDistrictList(@RequestParam @NotNull String city){
        return R.Success(sysPostService.getDistrictList(city));
    }

    /**
     * 取地址省市区Tree
     */
    @GetMapping("/getPostTree")
    public R<List<PostTreeResVo>> getPostTree(){
        return R.Success(sysPostService.getPostTree());
    }

    /**
     * 取分案批次历史GRID
     */
    @GetMapping("/fiDistBatchList")
    public R<PageData<DistBatchResVo>> fiDistBatchList(@Validated DistBatchReqVo distBatchReqVo){
        return R.SuccessPage(fiDistbatchHistService.distBatchList(distBatchReqVo));
    }

    /**
     * 取分案批次明细GRID
     */
    @GetMapping("/fiDistBatchDetailList")
    public R<PageData<DistBatchResVo>> fiDistBatchDetailList(@Validated DistBatchTailReqVo distBatchTailReqVo){
        return R.SuccessPage(fiDistbatchHistService.distBatchDetail(distBatchTailReqVo));
    }

    /**
     * 取分案批次历史ID.LIST
     */
    @GetMapping("/getDistBatchIDList")
    public R<List<LabelResVo>> getDistBatchList(){
        return R.Success(fiDistbatchHistService.getDistBatchList());
    }

    /**
     * 设置分案批次目标回收率
     */
    @PostMapping("/fiDistBatchSetAimRate")
    public R<CmdResVo> fiDistBatchSetAmiRate(@RequestBody @Validated BatchSetRateVo setRateVo) {
        String usrid= jCommonData.getCurrUsrID();
        return R.Success(fiDistbatchHistService.setAimRecyRate(usrid,setRateVo));
    }

    /**
     * 取罚扣项LIST
     */
    @GetMapping("/fiPenaltyList")
    public R<PageData<PenalResVo>> fiPenaltyList(@Validated PenalReqVo penalReqVo){
        return R.SuccessPage(fiPenaltyHistService.penaltyList(penalReqVo));
    }

    /**
     * 添加罚扣项
     */
    @PostMapping("/fiPenaltyInsert")
    public R<CmdResVo> fiPenaltyInsert(@RequestBody @Validated PenalEditVo penalEditVo) {
        String usrid= jCommonData.getCurrUsrID();
        return R.Success(fiPenaltyHistService.penaltyInsert(usrid,penalEditVo));
    }

    /**
     * 修改罚扣项
     */
    @PostMapping("/fiPenaltyUpdate")
    public R<CmdResVo> fiPenaltyUpdate(@RequestBody @Validated PenalEditVo penalEditVo) {
        String usrid= jCommonData.getCurrUsrID();
        return R.Success(fiPenaltyHistService.penaltyUpdate(usrid,penalEditVo));
    }

    /**
     * 删除罚扣项
     */
    @PostMapping("/fiPenaltyDelete")
    public R<CmdResVo> fiPenaltyDelete(@RequestBody @Validated DeleteReqVo deleteReqVo) {
        return R.Success(fiPenaltyHistService.penaltyDelete(Integer.parseInt(deleteReqVo.getId())));
    }

    /**
     * 取佣金比例LIST
     */
    @GetMapping("/fiCommissionList")
    public R<PageData<CmsResVo>> fiCommissionList(@Validated CmsReqVo cmsReqVo){
        return R.SuccessPage(fiCommissionService.commissionList(cmsReqVo));
    }

    /**
     * 添加佣金比例
     */
    @PostMapping("/fiCommissionInsert")
    public R<CmdResVo> fiCommissionInsert(@RequestBody @Validated CmsEditVo cmsEditVo) {
        String usrid= jCommonData.getCurrUsrID();
        return R.Success(fiCommissionService.commissionInsert(usrid,cmsEditVo));
    }

    /**
     * 修改佣金比例
     */
    @PostMapping("/fiCommissionUpdate")
    public R<CmdResVo> fiCommissionUpdate(@RequestBody @Validated CmsEditVo cmsEditVo) {
        String usrid= jCommonData.getCurrUsrID();
        return R.Success(fiCommissionService.commissionUpdate(usrid,cmsEditVo));
    }

    /**
     * 删除佣金比例
     */
    @PostMapping("/fiCommissionDelete")
    public R<CmdResVo> fiCommissionDelete(@RequestBody @Validated DeleteReqVo deleteReqVo) {
        return R.Success(fiCommissionService.commissionDelete(Integer.parseInt(deleteReqVo.getId())));
    }

    /**
     * 取有效时间段List
     */
    @GetMapping("/fiTimeLiveList")
    public R<PageData<TimeSetResVo>> fiTimeLiveList(@Validated TimeSetReqVo setReqVo){
        return R.SuccessPage(timeliveSetService.timeLiveList(setReqVo));
    }

    /**
     * 添加划扣有效时段
     */
    @PostMapping("/fiTimeLiveInsert")
    public R<CmdResVo> fiTimeLiveInsert(@RequestBody TimeSetEditVo timeSetVo) {
        String usrid= jCommonData.getCurrUsrID();
        return R.Success(timeliveSetService.timeLiveInsert(usrid,timeSetVo));
    }

    /**
     * 修改划扣有效时段
     */
    @PostMapping("/fiTimeLiveUpdate")
    public R<CmdResVo> fiTimeLiveUpdate(@RequestBody TimeSetEditVo timeSetVo) {
        String usrid= jCommonData.getCurrUsrID();
        return R.Success(timeliveSetService.timeLiveUpdate(usrid,timeSetVo));
    }

    /**
     * 删除划扣有效时段
     */
    @PostMapping("/fiTimeLiveDelete")
    public R<CmdResVo> fiTimeLiveDelete(@RequestBody DeleteReqVo reqVo) {
        return R.Success(timeliveSetService.timeLiveDelete(Integer.parseInt(reqVo.getId())));
    }
}
